lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'tree'], {
    data: {
        $layui: null,
        nodeList: [],
        treeData: [],
        seleNode: { id: 0 },
    },
    pageload: function (right) {
        console.log(right);
        this.initevent();
        var self = this;
        this.loadAllNode(function () {
            self.initData();
        });
    },
    //初始化事件
    initevent: function () {
        var self = this;
        // 监听表头工具栏
        layui.table.on("toolbar(List)", function (obj) {
            if (obj.event == "add") {
                if (self.data.seleNode.id == 0) {
                    layer.msg("请选择部位");
                    return;
                }
                self.viewEdit(null);
            }
        });
        //监听按钮事件
        layui.table.on('tool(List)', function (obj) {
            var data = obj.data;//当前行数据
            var layEvent = obj.event;//获得lay-event的值
            switch (layEvent) {
                case 'enstatus':
                    data.DelFlag = data.DelFlag == "0" ? 1 : 0;
                    self.savePositon(data);
                    break;
                case 'edit':
                    self.viewEdit(data);
                    break;
            }
        });
    },

    //加载内容
    loadAllNode: function (func) {
        //获取数据
        lobsterlayui.GETAPI('tsAdmin.getPositionList').then(retdata => {
            this.data.nodeList = retdata.positionList;
            func && func();

        })
    },
    //重新组装成tree数据
    initData: function () {
        var root = {
            id: 0,
            type: 0,
            title: '所有部位',
            selected: true,
            children: [],
            spread: true
        };
        //var list = this.data.nodeList.filter(item => item.NodeType == 1 || item.NodeType==2);
        var list = this.data.nodeList.filter(item => item.ParentId == 0);
        var currAdd = function (node, li) {
            var newnode = { id: node.ID, type: 2, title: node.PositionName, children: [], spread: true, selected: false }
            li.children.push(newnode)

        }
        list.forEach(item => {

            currAdd(item, root);
        });
        console.log(root)
        this.data.treeData = [root];
        this.viewToTree();
    },
    /**
     * */
    viewToTree: function () {
        var self = this;
        //渲染
        var inst1 = layui.tree.render({
            elem: '#Left',  //绑定元素
            isJump: false,
            onlyIconControl: true,
            data: this.data.treeData
            , click: function (obj) {
                console.log(obj.data); //得到当前点击的节点数据
                console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                console.log(obj.elem); //得到当前节点元素
                console.log(obj.data.children); //当前节点下是否有子节点
                //if (obj.data.id == 0) { return; }
                self.data.seleNode = obj.data;
                $("#Left .nodeover").removeClass("nodeover");
                $(obj.elem).find(".layui-tree-main").eq(0).addClass("nodeover");
                self.loadTable();
            }
        });
        this.loadTable();
    }
    ,
    viewEdit: function (data) {
        var self = this;

        layer.open({
            type: 1
            , title: '部位编辑'
            , btn: ['保存', "关闭"]
            , content: $('#openPosition').html()
            , area: ['500px', '300px']  //宽，高
            , cancel: function (index, layero) {
                layer.close(index);
            },
            success: function () {
                if (!data) {
                    data = { ID: 0, PositionCode: "", PositionName: "", DelFlag: 0, ParentId: self.data.seleNode.id };
                    $("#PositionCode").prop("disabled", false);
                } else {
                    $("#PositionCode").prop("disabled", true);
                }
                $("#PositionCode").val(data.PositionCode);
                $("#PositionName").val(data.PositionName);
            }
            , yes: function (index, layero) {
                data.PositionCode = $("#PositionCode").val();
                data.PositionName = $("#PositionName").val();
                self.savePositon(data, () => {
                    layer.close(index);
                })
            }
        });
    },
    /**
     * 保存
     * */
    savePositon: function (data, success) {
        if (!data.PositionCode || !data.PositionName) { return; }
        lobsterlayui.POSTAPI('tsAdmin.savePostion', data).then(retdata => {
            layer.msg("操作成功");
            this.loadAllNode(() => {
                this.loadTable();
            });
            success && success();
        })
    },
    /**
     * 加载表格
     * */
    loadTable: function () {
        var data = [];
        if (this.data.seleNode.id > 0) {
            data = this.data.nodeList.filter(a => a.ParentId == this.data.seleNode.id);
        } else {
            data = this.data.nodeList.filter(a => a.ParentId > 0);
        }
        console.log(data);
        layui.table.render({
            elem: "#List",
            headers: lobsterlayui.GetHeaderData(),
            toolbar: '#baradd',
            height: 'full-20',
            data: data,
            limit: '1000',
            //even: true,
            cols: [[
                { field: 'ID', title: '编号', width: '15%' },
                { field: 'PositionCode', title: '编码', width: '15%' },
                {
                    field: 'PositionName', title: '名称', width: '25%'
                },
                { field: 'DelFlag', title: '状态', align: 'center', templet: "#statusTemp", width: '15%' },
                { title: '操作', toolbar: '#operation', width: '30%', fixed: "right" }
            ]]
        });
    }
})